package com.uu.it.tracker.views;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;

import com.android.tracker.R;
import com.uu.it.tracker.db.DBAdapter;

public class ResultsView extends ExpandableListActivity implements OnChildClickListener{

	private ExpandableListAdapter mAdapter;   
	private static final String MONTH= "MONTH";
	private static final String WORKOUTS = "WORKOUTS";
	private static final String DATE = "DATE";
	private static final String START_TIME = "START_TIME";
	private static final String END_TIME = "END_TIME";
	
	Cursor c;
	DBAdapter db;
	 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
        //setContentView(R.layout.resultsview);
		
         db = new DBAdapter(this);
       
        /*Date now = new Date();
        SimpleDateFormat dfmt = new SimpleDateFormat("dd,MMM,yyyy");
        DateFormat tfmt = DateFormat.getTimeInstance(DateFormat.LONG);
        
        dfmt.format(now);
        tfmt.format(now);
        
        
        
        long id = db.insertHistory(dfmt.format(now), tfmt.format(now), "18:04:00", "1");
        db.close();
        
        db.open();
        //Cursor c = db.getAllHistory();
        if (c.moveToFirst()){
        	do {
        		DisplayHistory(c);
        	}while(c.moveToNext());
        }
        db.close(); */
                      
        
	}
	
	/*public void DisplayHistory(Cursor c) {
		Toast.makeText(this, "Date: " + c.getString(1) + "\n" + 
				"Start Time: " + c.getString(2) + "\n" +
				"End Time: " + c.getString(3) + "\n", 
				Toast.LENGTH_LONG).show();
	}*/
	protected void onStart() {
		super.onStart();
		 db.open();       
         c = db.getAllHistory();
        List<Map<String,String>> groupData = new ArrayList<Map<String,String>>();
        List<List<Map<String,String>>> childrenData = new ArrayList<List<Map<String,String>>>();
        
        if(c.moveToLast()){
        	List<Map<String,String>> children = new ArrayList<Map<String,String>>();
   
        	Map<String,String> curGroupMap = new HashMap<String,String>();
        	Map<String,String> curChildMap = new HashMap<String,String>();
        	String previousMonth = new String("");
        	int workouts = 0;
        	String date = new String("");
        	String month = new String("");
        	int index;
        	//first database record
        	date = c.getString(1);
    		index = date.indexOf(",")+1;
    		month = date.substring(index);
    
    		curChildMap.put(DATE, c.getString(1));
    		curChildMap.put(START_TIME, c.getString(2)+"\t"+c.getString(3));
    		//curChildMap.put(END_TIME, c.getString(3));
    		children.add(curChildMap);
			++workouts;
			previousMonth = month;
			
			while (c.moveToPrevious()){
				date = c.getString(1);
        		index = date.indexOf(",")+1;
        		month = date.substring(index);
        		
        		
        		//normal
        		if (!month.equals(previousMonth)){
        			childrenData.add(children);
        			children = new ArrayList<Map<String,String>>();
        			
        			curChildMap = new HashMap<String,String>();
        			children.add(curChildMap);
            		curChildMap.put(DATE, c.getString(1));
            		curChildMap.put(START_TIME, c.getString(2)+"\t"+c.getString(3));
            		//curChildMap.put(END_TIME, c.getString(3));
            		
            		curGroupMap = new HashMap<String,String>();
            		groupData.add(curGroupMap);
            		curGroupMap.put(MONTH, previousMonth+"\t"+workouts+" workouts");
            		//curGroupMap.put(WORKOUTS, ""+workouts+" workouts");	
        			workouts = 1;
        			previousMonth = month;
        			
        		}
        		else {
        			++workouts;
        		
        			curChildMap =new HashMap<String,String>();
        			children.add(curChildMap);
            		curChildMap.put(DATE, c.getString(1));
            		curChildMap.put(START_TIME, c.getString(2)+"\t"+c.getString(3));
            		//curChildMap.put(END_TIME, c.getString(3));
        		}
			}
        	
        	//last group
        	childrenData.add(children);
        	curGroupMap = new HashMap<String,String>();
    		groupData.add(curGroupMap);
    		curGroupMap.put(MONTH, previousMonth+"\t"+workouts+" workouts");
    		//curGroupMap.put(WORKOUTS, ""+workouts+" workouts");	
        }
        
        mAdapter = new SimpleExpandableListAdapter(
                this,
                groupData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] {MONTH, WORKOUTS },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childrenData,
                android.R.layout.simple_list_item_2,
                new String[] {DATE, START_TIME },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );
        setListAdapter(mAdapter);
        c.deactivate();
        //c.close();
        db.close();
	}
	
	protected void onRestart(){
		 super.onRestart();
		 c.requery();
		 
		 //startActivity(new Intent(this,ResultsView.class));
	 }
	 
	@Override
	public boolean onChildClick (ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
	{
		HashMap m = (HashMap) mAdapter.getChild(groupPosition, childPosition);
		String _date = (String)m.get(DATE);
		String time = (String)m.get(START_TIME);
		String _starttime = time.substring(0, time.indexOf("\t"));

		SharedPreferences preferences = getSharedPreferences("workouts", 0);
		SharedPreferences.Editor editor = preferences.edit();
        editor.putString("date", _date);
        editor.putString("starttime", _starttime);
        if(editor.commit()){
        	setResult(RESULT_OK);
        }
		startActivity(new Intent(ResultsView.this, WorkoutView.class));
		return true;	
	}
}
